/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.idgen.IdGen;
import com.jeesite.modules.pay.totalPay.util.StringUtil;
import com.jeesite.modules.sys.entity.Company;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.GoodsType;
import com.jeesite.modules.goods.entity.ZtCompany;
import com.jeesite.modules.goods.service.BrandService;
import com.jeesite.modules.goods.service.GoodsTypeService;
import com.jeesite.modules.goods.service.ZtCompanyService;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.jdProduct.utils.ProductPoolUtils;
import com.jeesite.modules.sys.entity.UserDataScope;
import com.jeesite.modules.util.AliRequestUtils;
import com.jeesite.modules.util.TTest;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztCompany")
public class ZtCompanyController extends BaseController {
	private String resultImg, imgName;
	
	@Autowired
	private ZtCompanyService ztCompanyService;
	@Autowired
	private ZtGoodsService ztGoodsService;
	@Autowired
	private BrandService brandService;
	@Autowired
	private MyProps myProps;
	@Autowired
	private GoodsTypeService goodsTypeService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtCompany get(String id, boolean isNewRecord) {

		ZtCompany ztCompany = new ZtCompany();
		ztCompany.setId(id);
		ztCompany = ztCompanyService.get(ztCompany);
		if (ztCompany == null) {
			ztCompany = new ZtCompany();
		}
		return ztCompany;
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ztCompany:ztCompanyData:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtCompany ztCompany, Model model) {
	
		model.addAttribute("ztCompany", ztCompany);

		return "modules/ztCompany/ztCompanyList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ztCompany:ztCompanyData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtCompany> listData(ZtCompany ztCompany,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		if(ztCompany==null){
			ztCompany=new ZtCompany();
		}
		
		Page<ZtCompany> page = ztCompanyService.findPage(new Page<ZtCompany>(
				request, response), ztCompany);

		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ztCompany:ztCompanyData:view")
	@RequestMapping(value = "form")
	public String form(ZtCompany ztCompany, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		
		GoodsType goodsType = new GoodsType();
		goodsType.setTreeLevel(0);
		
		goodsTypeService.addDataScopeFilter(goodsType, UserDataScope.CTRL_PERMI_MANAGE);
		List<GoodsType> goodscodeList = goodsTypeService.findList(goodsType);
		if(ztCompany.getCompany()==null){
			String companyStr = ztCompanyService.getCompany();
			String newCompany="000"+(Integer.valueOf(companyStr)+1);
			ztCompany.setCompany(newCompany);
		}
		model.addAttribute("ztCompany", ztCompany);
		model.addAttribute("goodscodeList", goodscodeList);
		return "modules/ztCompany/ztCompanyForm";
	}
	
	/**
	 * 保存数据
	 */
	@RequiresPermissions("ztCompany:ztCompanyData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtCompany ztCompany,HttpServletRequest request) {
		if(StringUtils.isBlank(ztCompany.getCompany())){
			ztCompany.setCreateDate(new Date());
		}
		
	/*	if(!ztCompany.getCompany().equals(ztCompany.getOldCompany())){
			ZtCompany ztc=ztCompanyService.getByCompany(ztCompany);
			if(ztc!=null && StringUtils.isNotBlank(ztc.getId())){
				return renderResult(Global.FALSE, text("公司编号存在！"));
			}
		}*/
		if(ztCompany.getImg().equals("")&& ztCompany.getImg().equals(null)){
			try {
				resultImg = TTest.getQcodeImg(request,ztCompany.getCompany(),"pages/index/index");

				System.out.println("========"+resultImg);

				String schemeUrl = myProps.getServerPath()+"/sk";
				resultImg=(schemeUrl+resultImg).replace("/sk/usr/local/tomcat8081/webapps", "");
				System.out.println("====88888===="+resultImg);
				int index=resultImg.lastIndexOf("/")+1;

				imgName=resultImg.substring(index);
				System.out.println("====8999999===="+imgName);
				ExecutorService cachedThreadPool=ProductPoolUtils.cachedThreadPool;

				if(cachedThreadPool==null){
					cachedThreadPool=Executors.newCachedThreadPool();
				}

				cachedThreadPool.execute(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						try{
							AliRequestUtils.uploadFile(resultImg, imgName,"linghu365");
						}catch(Exception ex){}
					}
				});

				ztCompany.setImg("https://linghu365.oss-cn-shenzhen.aliyuncs.com/"+imgName);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//pages/goodDel/goodDel

		}
		

		ztCompanyService.save(ztCompany);
		
		return renderResult(Global.TRUE, text("保存成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("ztCompany:ztCompanyData:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtCompany ztCompany) {
		ztCompanyService.delete(ztCompany);
		return renderResult(Global.TRUE, text("删除成功！"));
	}


}